package org.lele.utils;

import com.alibaba.excel.annotation.ExcelProperty;

import lombok.extern.slf4j.Slf4j;
import org.lele.excel.ExcelNotation;
import org.lele.excel.ExcelRequired;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ExcelUtils {

    /**
     * 获取必填列Map
     *
     * @param clazz 类class
     * @return java.util.Map<java.lang.Integer, java.lang.Short>
     * @author SunLingDa
     * @date 2022/11/3 13:23
     */
    public static Map<Integer, Short> getRequiredMap(Class<?> clazz) {
        Map<Integer, Short> requiredMap = new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAnnotationPresent(ExcelRequired.class)) {
                continue;
            }
            ExcelRequired excelRequired = field.getAnnotation(ExcelRequired.class);
            ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
            requiredMap.put(excelProperty.index(), excelRequired.frontColor().getIndex());
        }
        return requiredMap;
    }

    /**
     * 获取批注Map
     *
     * @param clazz 类class
     * @return java.util.Map<java.lang.Integer, java.lang.String>
     * @author SunLingDa
     * @date 2022/11/3 13:24
     */
    public static Map<Integer, String> getNotationMap(Class<?> clazz) {
        Map<Integer, String> notationMap =new HashMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (!field.isAnnotationPresent(ExcelNotation.class)) {
                continue;
            }
            ExcelNotation excelNotation = field.getAnnotation(ExcelNotation.class);
            ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
            notationMap.put(excelProperty.index(), excelNotation.value());
        }
        return notationMap;
    }


}
